<?php

if (!defined('ZZQSS')){
	die("Access Denied");
}

/**
 * InvoiceClass 发货单操作类
 * add()
 * del()
 * edit_state()
 * getdata()
 *
 */
class InvoiceClass {
	var $db;
	var $table;
	var $tablepre;
	function __construct(){
		//设置全局变量
		global $db,$tablepre;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."invoice";
	}
	function InvoiceClass() {
		$this->__construct();
	}
	/**
	 * 添加发货单
	 *
	 * @param unknown_type $oid
	 * @param unknown_type $freight
	 * @param unknown_type $delivery_fee
	 * @param unknown_type $consignee
	 * @param unknown_type $fname
	 * @param unknown_type $address
	 * @return OK/提示 
	 */
	function add($iarray){
		$timestamp=time();
		if(is_numeric($iarray['oid']) && !empty($iarray['consignee'])){
				$iarray['addtime']=$timestamp;
				$iarray['state']=0;
				if(is_numeric(inserttable($this->table, $iarray,1))){
					editstate("order","shipping_state","oid='{$iarray[oid]}'",1);//修改订单发货状态
					return "OK";
				}
				//$this->db->query("insert into {$this->table}(oid,order_sn,uid,uname,freight,delivery_fee,consignee,fname,address,addtime,state) VALUES({$oid},'{$row[order_sn]}','{$row[uid]}','{$row[uname]}','{$freight}','{$delivery_fee}','{$consignee}','{$fname}','{$address}',$timestamp,0)");						
				
		}else return "缺少参数";
	}
	/**
	 * 修改发货状态
	 *
	 * @param int $iid
	 * @param int $num
	 * @return return true/false
	 */
	function state($fid,$state=""){
		if($state===""){
			return $this->db->result_first("Select state From `$this->table` WHERE iid='{$fid}'");
		}elseif (($state==0 || $state==1  || $state==2)&&is_numeric($fid)){
			$this->db->query("update `$this->table` SET state = {$state} WHERE iid={$fid}");
			return true;
		}else return false;	
	}
	
	/**
	 * 删除指定ID运单
	 *
	 * @param int $iid
	 * @return true/false
	 */
	function del($iid){
		if(is_numeric($iid)){
			$this->db->query("delete from {$this->table} where iid={$iid}");
			return true;
		}return false;
	}
		/**
	 * 获取数据数组
	 *
	 * @param string $limit
	 * @param string $where
	 * @param string $orderby
	 * @return array
	 */
	function getdata($limit="",$where="",$orderby="")
	{
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(!empty($orderby))$orderby=" order by $orderby ";else $orderby=" order by iid desc";

		$sql="select * from {$this->table}{$where}{$orderby}{$limit}";
		$query =$this->db->query($sql);
		while($value = $this->db->fetch_array($query)) {
			//此处可进行数据整理
			$tempdata[]=$value;
		}
		return $tempdata;
	}
	function getcount($where=""){
		if(!empty($where))$where=" where $where";
		$count= $this->db->result_first("Select count(*) From `$this->table` $where");
		return $count;
	
	}
	
}

?>